/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: Graphics.java 583 2010-07-26 13:42:14Z Kent $
 *   $LastChangedDate: 2010-07-26 13:42:14 +0000 (Mon, 26 Jul 2010) $
 *   $LastChangedBy: Kent $
 *--------------------------------------------------------------------------
 */
package battlefield.resource;

import battlefield.entity.CellImage;
import battlefield.util.ImageUtils;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.swing.ImageIcon;

/**
 *
 * @author Harry Nguyen
 */
public class Graphics {

    private static HashMap<String, Graphics> graphics = new HashMap<String, Graphics>();
    public static final String GRAPHIC_DIRECTORY = "/battlefield/images/";
    public static final Graphics ICON_GAME = get(GRAPHIC_DIRECTORY + "frameIcon.png");
    public static final Graphics ICON_PREVIEW_ERROR = get(GRAPHIC_DIRECTORY + "error32x32.png");
    public static final Graphics BACKGROUND_MAIN = get(GRAPHIC_DIRECTORY + "gamePage.png");
    public static final Graphics BACKGROUND_MISC = get(GRAPHIC_DIRECTORY + "miscPage.png");
    public static final Graphics BACKGROUND_HOME = get(GRAPHIC_DIRECTORY + "homePage.png");
    public static final Graphics SCREEN_WIN = get(GRAPHIC_DIRECTORY + "screenWin.png");
    public static final Graphics SCREEN_LOOSE = get(GRAPHIC_DIRECTORY + "screenLose.png");
    public static final Graphics SCREEN_LOADING = get(GRAPHIC_DIRECTORY + "loading.png");
    public static final Graphics SCREEN_TITLE_CREATE_GAME = get(GRAPHIC_DIRECTORY + "createGame.png");
    public static final Graphics SCREEN_TITLE_GAME_OPTIONS = get(GRAPHIC_DIRECTORY + "gameOptions.png");
    public static final Graphics SCREEN_TITLE_JOIN_GAME = get(GRAPHIC_DIRECTORY + "joinGame.png");
    public static final Graphics SCREEN_TITLE_MAP_SELECTION = get(GRAPHIC_DIRECTORY + "mapSelection.png");
    public static final Graphics SCREEN_TITLE_WAIT_FOR_PLAYERS = get(GRAPHIC_DIRECTORY + "waitForPlayer.png");
    public static final Graphics BUTTON_MENU_NORMAL = get(GRAPHIC_DIRECTORY + "blackBtn.png");
    public static final Graphics BUTTON_MENU_HOVER = get(GRAPHIC_DIRECTORY + "blackBtnHover.png");
    public static final Graphics BUTTON_MENU_WHITE_NORMAL = get(GRAPHIC_DIRECTORY + "whiteBtn.png");
    public static final Graphics BUTTON_MENU_WHITE_HOVER = get(GRAPHIC_DIRECTORY + "whiteBtnHover.png");
    public static final Graphics BUTTON_TOGGLE_NORMAL = get(GRAPHIC_DIRECTORY + "toggleBtn.png");
    public static final Graphics BUTTON_TOGGLE_HOVER = get(GRAPHIC_DIRECTORY + "toggleBtn_hover.png");
    public static final Graphics BUTTON_SQUARE_NORMAL = get(GRAPHIC_DIRECTORY + "squareBtn.png");
    public static final Graphics BUTTON_SQUARE_HOVER = get(GRAPHIC_DIRECTORY + "squareBtn_hover.png");
    public static final Graphics BUTTON_READY_NORMAL = get(GRAPHIC_DIRECTORY + "playBtn.png");
    public static final Graphics BUTTON_READY_PRESSED = get(GRAPHIC_DIRECTORY + "playRollOverBtn.png");
    public static final Graphics BUTTON_HOME_NORMAL = get(GRAPHIC_DIRECTORY + "homeBtn.png");
    public static final Graphics BUTTON_HOME_PRESSED = get(GRAPHIC_DIRECTORY + "homeBtn_hover.png");
    public static final Graphics BUTTON_INVENTORY_NORMAL = get(GRAPHIC_DIRECTORY + "invBtn.png");
    public static final Graphics BUTTON_INVENTORY_HOVER = get(GRAPHIC_DIRECTORY + "invBtn_hover.png");
    public static final Graphics BUTTON_MISSION_NORMAL = get(GRAPHIC_DIRECTORY + "missionBG200x200.png");
    public static final Graphics BUTTON_MISSION_HOVER = get(GRAPHIC_DIRECTORY + "missionBG200x200_hover.png");
    public static final Graphics BUTTON_MISSION_NORMAL_SELECTED = get(GRAPHIC_DIRECTORY + "missionBG200x200_selected.png");
    public static final Graphics BUTTON_MISSION_HOVER_SELECTED = get(GRAPHIC_DIRECTORY + "missionBG200x200_hover_selected.png");
    public static final Graphics PANEL_MESSAGE = get(GRAPHIC_DIRECTORY + "msgBar.png");
    public static final Graphics PANEL_STATUS = get(GRAPHIC_DIRECTORY + "statusBar.png");
    public static final Graphics TAB_TITLE_INVENTORY = get(GRAPHIC_DIRECTORY + "tabInventory.png");
    public static final Graphics TAB_TITLE_MISSION = get(GRAPHIC_DIRECTORY + "tabMission.png");
    public static final Graphics BUTTON_NEXT_NORMAL = get(GRAPHIC_DIRECTORY + "nextBtn.png");
    public static final Graphics BUTTON_NEXT_PRESSED = get(GRAPHIC_DIRECTORY + "nextRollOverBtn.png");
    public static final Graphics BUTTON_VS_HARD = get(GRAPHIC_DIRECTORY + "vsHard.png");
    public static final Graphics BUTTON_VS_HARD_HOVER = get(GRAPHIC_DIRECTORY + "vsHard_hover.png");
    public static final Graphics BUTTON_VS_HARD_PRESSED = get(GRAPHIC_DIRECTORY + "vsHard_pressed.png");
    public static final Graphics BUTTON_VS_NORMAL = get(GRAPHIC_DIRECTORY + "vsNormal.png");
    public static final Graphics BUTTON_VS_NORMAL_HOVER = get(GRAPHIC_DIRECTORY + "vsNormal_hover.png");
    public static final Graphics BUTTON_VS_NORMAL_PRESSED = get(GRAPHIC_DIRECTORY + "vsNormal_pressed.png");
    public static final Graphics MAP_CONFIRMATION = get(GRAPHIC_DIRECTORY + "confirmMapMsg.png");
    public static final Graphics MAP_CONFIRMATION_RED = get(GRAPHIC_DIRECTORY + "confirmMapMsg_red.png");
    private BufferedImage bufferedImage;
    private Image image;
    private String source;

    public Graphics(String source) {
        this.source = source;
        this.image = new ImageIcon(this.getClass().getResource(source)).getImage();
    }

    public Graphics(String source, int width, int height) {
        this.source = source;
        this.image = ImageUtils.resize(get(source).getBufferedImage(), width, height);
    }

    public Graphics(String source, int x, int y, int width, int height) {
        this.source = source;
        image = ImageUtils.crop(get(source).getBufferedImage(), x, y, width, height);
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public Image getImage() {
        return image;
    }

    public BufferedImage getBufferedImage() {
        if (bufferedImage == null) {
            bufferedImage = ImageUtils.createBufferedImage(getImage());
        }

        return bufferedImage;
    }

    public Graphics resize(int width, int height) {
        return get(getSource(), width, height);
    }

    public Graphics crop(int x, int y, int width, int height) {
        return get(source, x, y, width, height);
    }

    public static Graphics get(String source) {
        if (graphics.containsKey(source)) {
            return graphics.get(source);
        }

        Graphics graphic = new Graphics(source);
        graphics.put(source, graphic);
        return graphic;
    }

    public static Graphics get(String source, int width, int height) {
        String key = String.format("%s@%s,%s", source, width, height);
        if (graphics.containsKey(key)) {
            return graphics.get(key);
        }


        Graphics graphic = new Graphics(source, width, height);
        graphics.put(key, graphic);
        return graphic;
    }

    public static Graphics get(String source, int x, int y, int width, int height) {
        String key = String.format("%s@%s,%s,%s,%s", source, x, y, width, height);
        if (graphics.containsKey(key)) {
            return graphics.get(key);
        }

        Graphics graphic = new Graphics(source, x, y, width, height);
        graphics.put(key, graphic);
        return graphic;
    }

    public static Graphics get(CellImage cellImage) {
        return get(cellImage.getSource(), cellImage.getX(), cellImage.getY(), cellImage.getWidth(), cellImage.getHeight());
    }
}
